use loco_rs::prelude::*;
use sea_orm::DeleteResult;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub use super::_entities::region_states::{self, ActiveModel, Entity, Model};

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateParams {
    pub region_id: String,
    pub country_id: String,
    pub state_id: String,
}

#[derive(Debug, Validate, Deserialize)]
pub struct Validator {
    #[validate(length(min = 1, message = "Region ID must not be empty."))]
    pub region_id: String,
    #[validate(length(min = 1, message = "Country ID must not be empty."))]
    pub country_id: String,
    #[validate(length(min = 1, message = "State ID must not be empty."))]
    pub state_id: String,
}

impl Validatable for ActiveModel {
    fn validator(&self) -> Box<dyn Validate> {
        Box::new(Validator {
            region_id: self.region_id.as_ref().to_owned(),
            country_id: self.country_id.as_ref().to_owned(),
            state_id: self.state_id.as_ref().to_owned(),
        })
    }
}

#[async_trait::async_trait]
impl ActiveModelBehavior for super::_entities::region_states::ActiveModel {
    async fn before_save<C>(self, _db: &C, insert: bool) -> Result<Self, DbErr>
    where
        C: ConnectionTrait,
    {
        self.validate()?;
        if insert {
            let mut this = self;
            this.id = ActiveValue::Set(Uuid::new_v4().to_string());
            Ok(this)
        } else {
            Ok(self)
        }
    }
}

impl Model {
    /// finds a region state by the provided id
    ///
    /// # Errors
    ///
    /// When could not find region state or DB query error
    pub async fn find_by_id(db: &DatabaseConnection, id: &str) -> ModelResult<Self> {
        let region_state = region_states::Entity::find()
            .filter(
                model::query::condition()
                    .eq(region_states::Column::Id, id)
                    .build(),
            )
            .one(db)
            .await?;
        region_state.ok_or_else(|| ModelError::EntityNotFound)
    }

    /// finds region states by the provided region id
    ///
    /// # Errors
    ///
    /// When could not find region states or DB query error
    pub async fn find_by_region_id(
        db: &DatabaseConnection,
        region_id: &str,
    ) -> ModelResult<Vec<Self>> {
        let region_states = region_states::Entity::find()
            .filter(
                model::query::condition()
                    .eq(region_states::Column::RegionId, region_id)
                    .build(),
            )
            .all(db)
            .await?;
        Ok(region_states)
    }

    /// finds region states by the provided country id
    ///
    /// # Errors
    ///
    /// When could not find region states or DB query error
    pub async fn find_by_country_id(
        db: &DatabaseConnection,
        country_id: &str,
    ) -> ModelResult<Vec<Self>> {
        let region_states = region_states::Entity::find()
            .filter(
                model::query::condition()
                    .eq(region_states::Column::CountryId, country_id)
                    .build(),
            )
            .all(db)
            .await?;
        Ok(region_states)
    }

    /// Asynchronously creates a region state and saves it to the database.
    ///
    /// # Errors
    ///
    /// When could not save the region state into the DB
    pub async fn create(db: &DatabaseConnection, params: &CreateParams) -> ModelResult<Self> {
        let region_state = region_states::ActiveModel {
            region_id: ActiveValue::set(params.region_id.to_string()),
            country_id: ActiveValue::set(params.country_id.to_string()),
            state_id: ActiveValue::set(params.state_id.to_string()),
            ..Default::default()
        }
        .insert(db)
        .await?;

        Ok(region_state)
    }

    /// Deletes the region state
    ///
    /// # Errors
    ///
    /// When could not delete the region state from the DB
    pub async fn delete(self, db: &DatabaseConnection) -> ModelResult<DeleteResult> {
        let model: region_states::ActiveModel = self.into();
        Ok(model.delete(db).await?)
    }
}

impl ActiveModel {}
